
//index.js
//获取应用实例
const app = getApp()
var tips;
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    date: '点击设置生日',
    avatar: '',
    nickname: '',
  },
  //事件处理函数

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

  var that = this;
  var openid = app.globalData.openid; //openid;

  app.userInfo.avatar = wx.getStorageSync('avater');
  var avatar = app.userInfo.avatar;

  app.userInfo.user_nicename = wx.getStorageSync('nickname');
  var nickname = app.userInfo.user_nicename;

  console.log(nickname); console.log(avatar); console.log('*---*');

  wx.request({
    url: 'https://www.cakeyouxuan.com/small/my/index',
    data: {
      openid: openid,
    },
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"  //post
    },
    success: function (res) {
      console.log(res);
      that.setData({
        list: res.data.list,
        idss: res.data.idss,
        avatar: avatar,
        nickname: nickname,
      });
    },
  })

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  //提交表单
  formSubmit:function(e){
    var address = e.detail.value.address;
    var phone = e.detail.value.phone;
    var radio = e.detail.value.radio;
    var years = e.detail.value.years;
    var uid = app.whereId.uid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/my/my_information',
      data: {
        uid: uid,
        address: address,
        phone: phone,
        radio: radio,
        years: years,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        if (res.data.msg == 'T') {
          wx.showToast({
            title: '信息修改成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  url: "/pages/yiguo/mine/mine",
                })
              }, 2000) //延迟时间
            }
          })
        }
      },
    })
  }

})



